package com.hzqc.wxaapp.sdk;

import org.springframework.http.ResponseEntity;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;

/**
 * @author xiafang
 * @since 2022/8/15 9:58
 */
public class WechatAppUrlSchemeService extends AbstractWechatAppService {
    private static final String ENDPOINT = "https://api.weixin.qq.com/wxa/generatescheme";

    public WechatAppUrlSchemeService(WechatAppTokenService wechatAppTokenService) {
        super(wechatAppTokenService);
    }

    public UrlSchemeResponse generateUrlScheme(UrlSchemeBody body) {
        URI uri = UriComponentsBuilder.fromHttpUrl(ENDPOINT)
                .build()
                .toUri();
        ResponseEntity<UrlSchemeResponse> response = this.client(body.getAppId())
                .postForEntity(uri, body, UrlSchemeResponse.class);
        UrlSchemeResponse responseBody = response.getBody();
        if (responseBody!=null){
            responseBody.setDays(body.getExpireInterval());
        }
        return responseBody;
    }
}
